<?php

namespace App\Models\Order;

use Illuminate\Database\Eloquent\Model;

class Saleorderinvoice extends Model
{
    protected $table = 'sale_order_invoice';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::orderBy('create_at', 'desc');

        $firm_name = $request->userName;
        if ($firm_name != '') {
            $query->where('firm_name', 'like', '%' . $firm_name . '%');
        }

        //manger 1自己添加的数据，2所有数据
        $manger = $request->manger;
        if ($manger == 1) {
            $query->where('manger_id', '=', $manger_id);
        }

        //1预开发票 2申请开票
        $type = $request->type;
        if ($type != '') {
            $query->where('type', '=', $type);
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        if ($sys_dpm_id > 0) {
            $query->where('sys_dpm_id', $sys_dpm_id);
        }

        $status = $request->status;
        if ($status > 0) {
            $query->where('status', $status);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
}
